package com.rush.springbootinit.aop;

import com.jd.platform.hotkey.client.callback.JdHotKeyStore;
import com.rush.springbootinit.annotation.DistributedLock;
import com.rush.springbootinit.annotation.HotKeyCheck;
import com.rush.springbootinit.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Slf4j
@Aspect
@Component
public class HotKeyCheckAspect {
    @Around("@annotation(hotKeyCheck)")
    public Object around(ProceedingJoinPoint point , HotKeyCheck hotKeyCheck) throws Throwable{
        String key1 = hotKeyCheck.key();
        //获取请求参数
        Object[] args = point.getArgs();
        for (Object arg : args){
            if (arg instanceof Long){
                String key = key1 + arg;
                //如果是热key
                if(JdHotKeyStore.isHotKey(key)){
                    //从本地缓存中获取缓存值
                    Object cacheDate = JdHotKeyStore.get(key);
                    if (cacheDate != null){
                        //如果有缓存值，直接返回缓存值
                        log.info("命中缓存");
                        return ResultUtils.success(cacheDate);
                    }
                }
            }
        }
        log.info("放行");
        return point.proceed();

    }
}
